package g11.action;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeMap;

import g11.mis.DrugTypeEnum;
import g11.mis.UserTypeEnum;
import g11.model.Employee;
import g11.model.Drug;
import g11.model.WorkFor;
import g11.dao.EmployeeDAO;
import g11.dao.DAOException;
import g11.dao.EmployeeDAOImpl;
import g11.dao.WorkForDAOImpl;
import g11.dao.WorkForDAO;
import g11.dao.DrugDAO;
import g11.dao.DrugDAOImpl;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport implements SessionAware,
		RequestAware {
	private String empType;
	private static final long serialVersionUID = 6066592355639005936L;
	private Map<String, Object> session;
	private Map<String, Object> request;

	private String selectedEmployeeId;

	private Employee retrievedEmployee;
	private List<Employee> retrievedDoctorList;
	private List<Employee> retrievedStaffList;
	private List<String> selectedWorkForDoctors; 
	/*
	 * retrievedEmployeeList is the different types of employees(the objects) in
	 * the system, will be used in initEmployeeListPage
	 */
	private List<Employee> retrievedEmployeeList;

	/*
	 * retrievedEmployees map<id, name> is the different types of employees in
	 * the system, will be used in initEmployeeListPage
	 */
	private Map<String, String> retrievedEmployees;

	/*
	 * retrievedDoctors map<id, name> is all the doctors in the system, will be
	 * used when do createWorkFor
	 */
	private Map<String, String> retrievedDoctors;
	/*
	 * retrievedStaffs map<id,name> is all the staffs in the system, will be
	 * used when do createWorkFor
	 */
	private Map<String, String> retrievedStaffs;

	/* retrievedDrug is the currentDrug in session */
	private Drug retrievedDrug;
	/* selectedDrugId is the currentDrug's ID for update */
	private String selectedDrugId;
	//retrievedDrugList
	private List<Drug> retrievedDrugList;

	private final Logger log = Logger.getLogger(this.getClass());

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map request) {
		this.request = request;
	}
	
	public List<Employee> getRetrievedDoctorList() {
		return retrievedDoctorList;
	}

	public void setRetrievedDoctorList(List<Employee> retrievedDoctorList) {
		this.retrievedDoctorList = retrievedDoctorList;
	}

	public List<Employee> getRetrievedStaffList() {
		return retrievedStaffList;
	}

	public void setRetrievedStaffList(List<Employee> retrievedStaffList) {
		this.retrievedStaffList = retrievedStaffList;
	}

	public Employee getRetrievedEmployee() {
		return retrievedEmployee;
	}

	public void setRetrievedEmployee(Employee retrievedEmployee) {
		this.retrievedEmployee = retrievedEmployee;
	}

	public List<String> getSelectedWorkForDoctors() {
		return selectedWorkForDoctors;
	}

	public void setSelectedWorkForDoctors(List<String> selectedWorkForDoctors) {
		this.selectedWorkForDoctors = selectedWorkForDoctors;
	}

	public String getSelectedEmployeeId() {
		return selectedEmployeeId;
	}

	public void setSelectedEmployeeId(String selectedEmployeeId) {
		this.selectedEmployeeId = selectedEmployeeId;
	}

	/**
	 * @return the empType
	 */
	public String getEmpType() {
		return empType;
	}

	/**
	 * @param empType
	 *            the empType to set
	 */
	public void setEmpType(String empType) {
		this.empType = empType;
	}

	/**
	 * @return the retrievedEmployeeList
	 */
	public List<Employee> getRetrievedEmployeeList() {
		return retrievedEmployeeList;
	}

	/**
	 * @param retrievedEmployeeList
	 *            the retrievedEmployeeList to set
	 */
	public void setRetrievedEmployeeList(List<Employee> retrievedEmployeeList) {
		this.retrievedEmployeeList = retrievedEmployeeList;
	}

	/**
	 * @return the retrievedEmployees
	 */
	public Map<String, String> getRetrievedEmployees() {
		return retrievedEmployees;
	}

	/**
	 * @param retrievedEmployees
	 *            the retrievedEmployees to set
	 */
	public void setRetrievedEmployees(Map<String, String> retrievedEmployees) {
		this.retrievedEmployees = retrievedEmployees;
	}

	/**
	 * @return the retrievedDoctors
	 */
	public Map<String, String> getRetrievedDoctors() {
		return retrievedDoctors;
	}

	/**
	 * @param retrievedDoctors
	 *            the retrievedDoctors to set
	 */
	public void setRetrievedDoctors(Map<String, String> retrievedDoctors) {
		this.retrievedDoctors = retrievedDoctors;
	}

	/**
	 * @return the retrievedStaffs
	 */
	public Map<String, String> getRetrievedStaffs() {
		return retrievedStaffs;
	}

	/**
	 * @param retrievedStaffs
	 *            the retrievedStaffs to set
	 */
	public void setRetrievedStaffs(Map<String, String> retrievedStaffs) {
		this.retrievedStaffs = retrievedStaffs;
	}

	/**
	 * @return the retrievedDrug
	 */
	public Drug getRetrievedDrug() {
		return retrievedDrug;
	}

	/**
	 * @param retrievedDrug
	 *            the retrievedDrug to set
	 */
	public void setRetrievedDrug(Drug retrievedDrug) {
		this.retrievedDrug = retrievedDrug;
	}

	/**
	 * @return the selectedDrugId
	 */
	public String getSelectedDrugId() {
		return selectedDrugId;
	}

	/**
	 * @param selectedDrugId
	 *            the selectedDrugId to set
	 */
	public void setSelectedDrugId(String selectedDrugId) {
		this.selectedDrugId = selectedDrugId;
	}

	public List<Drug> getRetrievedDrugList() {
		return retrievedDrugList;
	}

	public void setRetrievedDrugList(List<Drug> retrievedDrugList) {
		this.retrievedDrugList = retrievedDrugList;
	}

	/*
	 * @param nothing
	 * 
	 * @return Map<String, String> retrievedDoctors //all doctor map
	 */
	public String initStaffCreatePage() {
		session.put("triggeredAction", "initStaffCreatePage");
		/*
		 * create an empty Employee, set its type to STAFF
		 */
		this.retrievedEmployee = new Employee();
		this.retrievedEmployee.setType((byte) UserTypeEnum.STAFF.index());

		/*
		 * set retrievedDoctorList to all doctor list, because need setting
		 * workFor doctors for this staff
		 */
		EmployeeDAO empDao = new EmployeeDAOImpl();
		try {
			this.retrievedDoctorList = empDao.getAllEmployee("doctor");

		} catch (DAOException e) {
			log.error(
					"Error: EmployeeAction: initStaffCreatePage():getAllDoctor()",
					e);
			return ERROR;
		} finally {

		}
		session.put("actionResult","");
		return SUCCESS;
	}

	/*
	 * @param String selectedEmployeeId set retrievedEmployee
	 * (getEmployeeById(staffId)) set selectedWorkForDoctors (get all doctors
	 * this staff is working for); set retrievedDoctors (get all doctors list
	 * and put them into Map<id,name>)
	 */
	public String initStaffUpdatePage() {
		session.put("triggeredAction", "initStaffUpdatePage");
		/*
		 * first get the Employee
		 */
		EmployeeDAO empDao = new EmployeeDAOImpl();
		int staffId = Integer.valueOf(selectedEmployeeId);
		Employee staff = null;
		try {
			staff = empDao.getEmployeeById(staffId);
		} catch (DAOException e) {
			log.error(
					"Error: EmployeeAction: initStaffUpdatePage():getEmployeeById",
					e);
			return ERROR;

		}
		
		if (staff == null)
			return ERROR;
		/*
		 * set retrievedEmployee
		 */
		this.retrievedEmployee = staff;

		WorkForDAO wfDao = new WorkForDAOImpl();
		List<Employee> tempDocList = new ArrayList<Employee>();
		try {
			tempDocList = wfDao.getDoctorList(staff);
		} catch (DAOException e) {
			log.error(
					"Error: EmployeeAction: initStaffUpdatePage():getEmployeeById",
					e);
			return ERROR;

		}
		
		this.selectedWorkForDoctors = new ArrayList<String>();
		if (!tempDocList.isEmpty()) {
			/*
			 * this staff has workfor doctors, then set selectedWorkForDoctors
			 */
			for (Employee e : tempDocList) {
				this.selectedWorkForDoctors.add(String.valueOf(e.getId()));
			}
		}
		
		/*
		 * need get all doctors and set retrievedDoctorList
		 */
		try {
			this.retrievedDoctorList = empDao.getAllEmployee("doctor");
		} catch (DAOException e) {
			log.error(
					"Error: EmployeeAction: initStaffUpdatePage():getAllDoctor()",
					e);
			return ERROR;

		}
		session.put("actionResult","");
		return SUCCESS;
	}

	/*
	 * @param Nothing
	 * 
	 * @return Employee retrievedEmployee //an empty doctor
	 */
	public String initEmployeeCreatePage() {
		session.put("triggeredAction", "initDoctorCreatePage");
		/* an empty Employee */
		this.retrievedEmployee = new Employee();
		/* set its type to Doctor */
//		this.retrievedEmployee.setType((byte) UserTypeEnum.DOCTOR.index());
		session.put("actionResult","");
		return SUCCESS;
	}

	/*
	 * @param String selectedEmployeeId
	 * 
	 * @return Employee retrievedEmployee
	 */
	public String initEmployeeUpdatePage() {
		session.put("triggeredAction", "initDoctorUpdatePage");
		/*
		 * need getEmployeeById
		 */
		EmployeeDAO epDao = new EmployeeDAOImpl();
		int employeeID = Integer.valueOf(this.selectedEmployeeId);
		try {
			this.retrievedEmployee = epDao.getEmployeeById(employeeID);
		} catch (DAOException e) {
			log.error(
					"Error: EmployeeAction: initDoctorUpdatePage():getEmployeeById",
					e);
			return ERROR;
		}
		
		session.put("actionResult","");
		return SUCCESS;
	}

	/*
	 * @param empType-the employee type:Doctor, Staff, Legal, Finance
	 * 
	 * @return List<Employee> retrievedEmployeeList
	 * 
	 * @return Map<String,String> retrievedEmployees;
	 * 
	 * @return Map<String,String>
	 */
	public String initEmployeeListPage() {
		session.put("triggeredAction", "initEmployeeListPage");

		log.info("To list all " + empType + " employees");

		EmployeeDAO empDao = new EmployeeDAOImpl();
		try {
			this.retrievedEmployeeList = empDao.getAllEmployee(empType);

		} catch (DAOException e) {
			log.error(
					"Error: EmployeeAction: initEmployeeListPage():getAllEmployee",
					e);
			return ERROR;
		}
		if (!this.retrievedEmployeeList.isEmpty()) {
			this.retrievedEmployees = new TreeMap<String, String>();
			/*
			 * put retrievedEmployeeList into Map(String,String)
			 * retrievedEmployees
			 */
			for (Employee e : this.retrievedEmployeeList) {
				this.retrievedEmployees.put(String.valueOf(e.getId()),
						e.getName());
			}
		}

		return SUCCESS;
	}
	
	/*
	 * Admin edit all employee's info; specifically if this employee is staff, list all this staff's workFor 
	 * doctors and can change the workfor doctors
	 */
	public String initEmployeeEditPage(){
		/*to do
		 * 
		 */
		return SUCCESS;
	}

	/*
	 * All drug's info for admin staff to create and edit; 
	 */
	public String initDrugListPage(){
		DrugDAOImpl dDao = new DrugDAOImpl();
		
		try {
			this.retrievedDrugList = dDao.getAllDrugs();
		} catch (DAOException e) {
			e.printStackTrace();
			return ERROR;
		}
		
		
		return SUCCESS;
	}
	
	
	/*
	 * @param Employee this.retrievedEmployee List<String>
	 * selectedWorkForDoctors //getSelectedWorkForDoctors
	 * 
	 * @return Employee this.retrievedEmployee //the new created staff
	 */
	public String createStaff() {
		session.put("triggeredAction", "createStaff");
		/*
		 * persist retrievedEmployee through EmployeeDAO first
		 */
		EmployeeDAO epDao = new EmployeeDAOImpl();
		retrievedEmployee.setType((byte) UserTypeEnum.STAFF.index());
		Integer employeeId = new Integer(0);
		this.empType = "STAFF";
		try {
			employeeId = epDao.createEmployee(retrievedEmployee);
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: createStaff():createEmployee", e);
			session.put("actionResult",e.getMessage());
			/*
			 * set retrievedDoctorList to all doctor list, because need setting
			 * workFor doctors for this staff
			 */
			EmployeeDAO empDao = new EmployeeDAOImpl();
			try {
				this.retrievedDoctorList = empDao.getAllEmployee("doctor");

			} catch (DAOException e1) {
				log.error(
						"Error: EmployeeAction: initStaffCreatePage():getAllDoctor()",
						e1);
				return ERROR;
			} finally {

			}
			return ERROR;
		} finally {

		}
		
		/*
		 * Also need persist the workfor sets
		 */
		if (!selectedWorkForDoctors.isEmpty()) {
			/*
			 * if workfor doctors are chosen for this staff
			 */
			Set<String> workForDoctorSet = new HashSet<String>(this.selectedWorkForDoctors);
			WorkForDAO wkfDao = new WorkForDAOImpl();
			for (String s : workForDoctorSet) {
				Integer doctorID = Integer.valueOf(s);
				try {
					Employee doctor = epDao.getEmployeeById(doctorID);
					wkfDao.createWorkForRelation(doctor, retrievedEmployee);
				} catch (DAOException e) {
					log.error("Error: EmployeeAction: " + "createStaff(): ", e);
					return ERROR;
				} finally {

				}
			}
		}
		//retrieve new work for doctors
		this.selectedWorkForDoctors = this.retrieveWorkForDoctorName(retrievedEmployee);
		session.put("actionResult","Staff "+retrievedEmployee.getName()+" successfully created !!");
//		this.empType = "STAFF";
		return SUCCESS;
	}

	/*
	 * @param Employee this.retrievedEmployee
	 * 
	 * @return Employee this.retrievedEmployee // the new created doctor
	 */
	public String createDoctor() throws DAOException {
		session.put("triggeredAction", "createDoctor");
		this.empType = "DOCTOR";
		/*
		 * to make sure the Employee has a doctor type
		 */
		retrievedEmployee.setType((byte) UserTypeEnum.DOCTOR.index());
		/*
		 * persist retrievedEmployee through EmployeeDAO
		 */
		try {
			EmployeeDAO epDao = new EmployeeDAOImpl();
			epDao.createEmployee(retrievedEmployee);
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "createDocotr(): createEmployee", e);
			session.put("actionResult",e.getMessage());
			return ERROR;
		}
		session.put("actionResult","Doctor "+retrievedEmployee.getName()+" successfully created !!");
		return SUCCESS;
	}
	
	/*
	 * @param Employee this.retrievedEmployee
	 * 
	 * @return Employee this.retrievedEmployee // the new created finance
	 */
	public String createFinance() throws DAOException {
		session.put("triggeredAction", "createFinance");
		this.empType = "FINANCE";
		/*
		 * to make sure the Employee has a doctor type
		 */
		retrievedEmployee.setType((byte) UserTypeEnum.FINANCE.index());
		/*
		 * persist retrievedEmployee through EmployeeDAO
		 */
		try {
			EmployeeDAO epDao = new EmployeeDAOImpl();
			epDao.createEmployee(retrievedEmployee);
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "createDocotr(): createEmployee", e);
			session.put("actionResult",e.getMessage());
			return ERROR;
		}
		session.put("actionResult","Finance "+retrievedEmployee.getName()+" successfully created !!");
		return SUCCESS;
	}
	
	/*
	 * @param Employee this.retrievedEmployee
	 * 
	 * @return Employee this.retrievedEmployee // the new created legal usr
	 */
	public String createLegal() throws DAOException {
		session.put("triggeredAction", "createLegal");
		this.empType = "LEGAL";
		/*
		 * to make sure the Employee has a doctor type
		 */
		retrievedEmployee.setType((byte) UserTypeEnum.LEGAL.index());
		/*
		 * persist retrievedEmployee through EmployeeDAO
		 */
		try {
			EmployeeDAO epDao = new EmployeeDAOImpl();
			epDao.createEmployee(retrievedEmployee);
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "createDocotr(): createEmployee", e);
			session.put("actionResult",e.getMessage());
			return ERROR;
		}
		session.put("actionResult","Legal "+retrievedEmployee.getName()+" successfully created !!");
		return SUCCESS;
	}
	
	
	/*
	 * update employee except staff
	 */
	public String updateEmployee(){
		
		Integer employeeId = retrievedEmployee.getId();
		EmployeeDAOImpl eDao = new EmployeeDAOImpl();
		Employee employee = new Employee();
		
		try {
			employee = eDao.getEmployeeById(employeeId);
		} catch (DAOException e) {
			e.printStackTrace();
		}
		
		//update password if changed
		if(retrievedEmployee.getPassword().length() != 0){
			employee.setPassword(retrievedEmployee.getPassword());
		}
		employee.setDepartment(retrievedEmployee.getDepartment());
		employee.setName(retrievedEmployee.getName());
		employee.setTitle(retrievedEmployee.getTitle());
		
		try {
			eDao.updateEmployee(employee);
		} catch (DAOException e) {
			e.printStackTrace();
			session.put("actionResult",e.getMessage());
			return ERROR;
		}
		
		if(employee.getType() == (byte)UserTypeEnum.STAFF.index()){
			WorkForDAO wkfDao = new WorkForDAOImpl();
			EmployeeDAOImpl epDao = new EmployeeDAOImpl();
			//First, delete old workFor relations
				for(WorkFor wf:employee.getWorkforSet()){
					try {
						wkfDao.deleteWorkForRelationByWorkFor(wf);
					} catch (DAOException e) {
						e.printStackTrace();
						session.put("actionResult",e.getMessage());
						return ERROR;
					}
				}
			
			 //Second, persist new workfor sets
			if (selectedWorkForDoctors!=null) {
				/*
				 * if workfor doctors are chosen for this staff
				 */
				Set<String> workForDoctorSet = new HashSet<String>(this.selectedWorkForDoctors);
				for (String s : workForDoctorSet) {
					Integer doctorID = Integer.valueOf(s);
					try {
						Employee doctor = epDao.getEmployeeById(doctorID);
						wkfDao.createWorkForRelation(doctor, retrievedEmployee);
					} catch (DAOException e) {
						log.error("Error: EmployeeAction: " + "createStaff(): ", e);
						session.put("actionResult",e.getMessage());
						return ERROR;
					} finally {

					}
				}
			}
			
			//retrieve new work for doctors
			this.selectedWorkForDoctors = this.retrieveWorkForDoctorName(employee);
		}
		
		
		switch(employee.getType()){
			case 1:	session.put("actionResult","Doctor "+employee.getName()+" successfully updated !!");break;
			case 2:	session.put("actionResult","Staff "+employee.getName()+" successfully updated !!");break;
			case 3:	session.put("actionResult","Finance "+employee.getName()+" successfully updated !!");break;
			case 4:	session.put("actionResult","Legal "+employee.getName()+" successfully updated !!");break;
		}
		
		this.retrievedEmployee = employee;
		
		return SUCCESS;
	}
	
	private List<String> retrieveWorkForDoctorName(Employee staff){
		//retrieve new work for doctors
		WorkForDAO wfDao = new WorkForDAOImpl();
		List<Employee> tempDocList = new ArrayList<Employee>();
		try {
			tempDocList = wfDao.getDoctorList(staff);
		} catch (DAOException e) {
			log.error(
					"Error: EmployeeAction: initStaffUpdatePage():getEmployeeById",
					e);
			
		} finally {

		}
		
		List<String> selectedWorkForDoctorNames = new ArrayList<String>();
		if (!tempDocList.isEmpty()) {
			/*
			 * this staff has workfor doctors, then set selectedWorkForDoctors
			 */
			for (Employee e : tempDocList) {
				selectedWorkForDoctorNames.add(e.getName());
			}
		}
		return selectedWorkForDoctorNames;
	}
	
	/*
	 * @param none
	 * 
	 * @return retrievedDrug: an empty Drug object
	 */
	public String InitCreateDrugPage() throws DAOException {

		this.retrievedDrug = new Drug();
		return SUCCESS;
	}

	/*
	 * @param retrievedDrug: the empty Drug object persist retrievedDrug
	 * 
	 * @return
	 */
	public String CreateDrug() throws DAOException {
		/* create a temp drug */
		Drug tempDrug = new Drug();
		tempDrug.setDrugName(retrievedDrug.getDrugName());
		tempDrug.setType(retrievedDrug.getType());
		/* try to create this temp drug */
		DrugDAO drgDao = new DrugDAOImpl();
		try {
			drgDao.createDrug(tempDrug);
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "CreateDrug(): createDrug(tempDrug)", e);
			return ERROR;
		}
		/* try to get the drug from DAO, so that it has a Id */
		try {
			retrievedDrug = drgDao.getDrugByName(tempDrug.getDrugName());
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "CreateDrug(): getDrugByName()", e);
			return ERROR;
		}
		/* also need to set selectedDrugId to this new drug's ID */
		this.selectedDrugId = String.valueOf(retrievedDrug.getDrugId());
		return SUCCESS;
	}

	/*
	 * @param selectedDrugId: the selected Drug(for update)'s ID persist this
	 * Drug and set retrievedDrug to this updated Drug
	 * 
	 * @param retrievedDrug: the updated Drug( the name and type updated by
	 * user)
	 * 
	 * @return
	 */
	public String UpdateDrug() throws DAOException {
		/* get the drug from DAO and save it to a temp Drug */
		Drug tempDrug = new Drug();
		DrugDAO drgDao = new DrugDAOImpl();

		try {
			tempDrug = drgDao.getDrugById(Integer.valueOf(selectedDrugId));
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: " + "UpdateDrug(): getDrugById",
					e);
			return ERROR;
		}

		/* update the temp drug */
		tempDrug.setDrugName(retrievedDrug.getDrugName());
		tempDrug.setType(retrievedDrug.getType());

		/* try to update this temp drug in DAO */
		try {
			drgDao.updateDrug(tempDrug);
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: " + "UpdateDrug(): UpdateDrug()",
					e);
			return ERROR;
		}
		/* need to update retrievedDrug */
		this.retrievedDrug = tempDrug;
		return SUCCESS;
	}

	/*
	 * @param none
	 * 
	 * @return Map(String, String) retrievedStaffs: list of all the staffs
	 * 
	 * @return Map(String, String) retrievedDoctors: list of all the doctors
	 */
	public String InitcreateWorkForPage() throws DAOException {
		session.put("triggeredAction", "InitcreateWorkForPage");
		/*
		 * to get all doctors object from Employee table
		 */
		EmployeeDAO empDao = new EmployeeDAOImpl();
		List<Employee> doctors = new ArrayList<Employee>();
		try {
			doctors = empDao.getAllEmployee("doctor");
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "InitcreateWorkForPage(): getAllEmployee(doctor)", e);
			return ERROR;
		}
		/*
		 * to put all the doctors into Map
		 */
		for (Employee e : doctors) {
			retrievedDoctors.put(String.valueOf(e.getId()), e.getName());
		}

		/*
		 * to get all staffs object from Employee table
		 */
		List<Employee> staffs = new ArrayList<Employee>();
		try {
			staffs = empDao.getAllEmployee("staff");
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "InitcreateWorkForPage(): getAllEmployee(staff)", e);
			return ERROR;
		}
		/*
		 * to put all the staffs into Map
		 */
		for (Employee e : staffs) {
			this.retrievedStaffs.put(String.valueOf(e.getId()), e.getName());
		}

		return SUCCESS;
	}

	/*
	 * @param String selectedEmployeeId- the staff ID
	 * 
	 * @param List<String> selectedWorkForDoctors - the doctor's IDs create
	 * workfor relationship in WorkFor table for this staffID and doctor's IDs
	 * 
	 * @
	 */
	public String createWorkFor() throws DAOException {
		session.put("triggeredAction", "createWorkFor");
		/*
		 * to get an Employee object from selectedEmployeeId
		 */
		EmployeeDAO empDao = new EmployeeDAOImpl();
		Employee staff = new Employee();
		try {
			staff = empDao.getEmployeeById(Integer
					.valueOf(this.selectedEmployeeId));
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "createWorkFor(): getEmployeeById for staff", e);
			return ERROR;
		}
		/*
		 * to get List<Employee> from selectedWorkForDoctors
		 */
		List<Employee> doctors = new ArrayList<Employee>();
		Employee tempDoc = new Employee();
		for (String s : this.selectedWorkForDoctors) {
			try {
				tempDoc = empDao.getEmployeeById(Integer.valueOf(s));
				doctors.add(tempDoc);
			} catch (DAOException e) {
				log.error("Error: EmployeeAction: "
						+ "createWorkFor(): getEmployeeById for doctors", e);
				return ERROR;
			}
		}
		/*
		 * persist workFor relationship to workFor table
		 */
		WorkForDAO wrkfrDao = new WorkForDAOImpl();
		try {
			for (Employee tempdoc : doctors) {
				wrkfrDao.createWorkForRelation(tempdoc, staff);
			}
		} catch (DAOException e) {
			log.error("Error: EmployeeAction: "
					+ "createWorkFor(): createWorkForRelation", e);
			return ERROR;
		}
		return SUCCESS;
	}

	public Map<String,String> getDrugTypeMap(){
		Map<String,String> typeMap = new HashMap<String,String>();
		for(DrugTypeEnum dte: DrugTypeEnum.values()){
			typeMap.put(String.valueOf(dte.index()), dte.toString().toLowerCase());
		}
		return typeMap;
	}

}
